package com.example.his.api.mis.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.UserDao;
import com.example.his.api.db.pojo.UserEntity;
import com.example.his.api.mis.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huixh
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    // 加密username转成 MD5 前6 个字符 加密码 + 后3 个字符 在转md5
    @Override
    public Integer login(Map<String, Object> param) {
        String username = MapUtil.getStr(param, "username");
        String password = MapUtil.getStr(param, "password");
        param.replace("password", createHaxPassword(username,password));
        return userDao.login(param);
    }

    @Override
    public int updatePassword(Map<String, Object> param) {
        int userId = MapUtil.getInt(param, "userId");
        String username = userDao.searchUserNameById(userId);
        String password = MapUtil.getStr(param, "password");
        param.replace("password", createHaxPassword(username,password));
        String newPassword = MapUtil.getStr(param, "newPassword");
        param.replace("newPassword", createHaxPassword(username,newPassword));
        return userDao.updatePassword(param);
    }

    private String createHaxPassword(String username,String password){
        MD5 md5 = MD5.create();
        String temp = md5.digestHex(username);
        String tempStart = StrUtil.subWithLength(temp, 0, 6);
        String tempEnd = StrUtil.subSuf(temp, temp.length() - 3);
        return md5.digestHex(tempStart + password + tempEnd).toUpperCase();
    }

    @Override
    public PageUtils searchByPage(Map<String, Object> param) {
        List<?> list = new ArrayList<>();
        long count = userDao.searchCount(param);
        if (count > 0) {
            list = userDao.searchByPage(param);
        }
        int page = MapUtil.getInt(param, "page");
        int length = MapUtil.getInt(param, "length");

        return new PageUtils(count, length, page, list);
    }

    @Override
    @Transactional
    public int insert(UserEntity userEntity) {
        String haxPassword = createHaxPassword(userEntity.getUsername(), userEntity.getPassword());
        userEntity.setPassword(haxPassword);
        return userDao.insert(userEntity);
    }

    @Override
    public HashMap<?, ?> searchById(int userId) {
        return userDao.searchById(userId);
    }

    @Override
    @Transactional
    public int update(Map<String, Object> param) {
        return userDao.update(param);
    }

    @Override
    @Transactional
    public int deleteByIds(Integer[] ids) {
        if (ids != null && ids.length > 0) {
            return userDao.deleteByIds(ids);
        }
        return 0;
    }
}
